package joyi.train.employee.qmd;

import j.framework.core.LoginUser;
import j.framework.qmd.core.QueryDataHandler;
import j.framework.qmd.core.QueryFieldMetadata;
import j.framework.qmd.core.QueryMetadata;
import j.framework.qmd.core.QueryResult;
import j.util.SpringBeans;
import j.util.StringUtils;
import j.util.jdbc.JdbcTemplate;
import j.util.jdbc.QueryJdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WY2015092804 on 2015/10/20.
 */

public class EmployeeQueryHandler implements QueryDataHandler {

    private JdbcTemplate jdbcTemplate = SpringBeans.getBean(JdbcTemplate.class);

    private Map<String,Object> positionNameMap;  //职位Id与职位name的map集合

    private Map<String,Object> departmentNameMap;  //部门Id与部门name的map集合

    public EmployeeQueryHandler(){
        List<Map<String,Object>> list1 = jdbcTemplate.queryForListMap("select * from cw_Position");
        positionNameMap = new HashMap<>();
        for (Map<String,Object> map : list1){
            positionNameMap.put(map.get("positionId").toString(),map.get("positionName"));
        }
        List<Map<String,Object>> list2 = jdbcTemplate.queryForListMap("select * from cw_Department");
        departmentNameMap = new HashMap<>();
        for (Map<String,Object> map : list2){
            departmentNameMap.put(map.get("departmentId").toString(),map.get("departmentName"));
        }
    }


//    private Map<String,Object> getPositionNameMap(){
//        List<Map<String,Object>> list = jdbcTemplate.queryForListMap("select * from cw_Position");
//        positionNameMap = new HashMap<>();
//        for (Map<String,Object> map : list){
//            positionNameMap.put(map.get("positionId").toString(),map.get("positionName"));
//        }
//        return positionNameMap;
//    }
//
//    public Map<String,Object> getDepartmentNameMap() {
//        List<Map<String,Object>> list = jdbcTemplate.queryForListMap("select * from cw_Department");
//        departmentNameMap = new HashMap<>();
//        for (Map<String,Object> map : list){
//            departmentNameMap.put(map.get("departmentId").toString(),map.get("departmentName"));
//        }
//        return departmentNameMap;
//    }
    @Override
    public Object getColumnValue(QueryFieldMetadata fieldMetadata, Object value, Map<String, Object> rowData) {
        if ("gender".equals(fieldMetadata.getFieldName())) {
            if ("1".equals(value)) {
                return "男";
            } else if ("0".equals(value)) {
                return "女";
            } else {
                return "未知";
            }
        } else if ("positionId".equals(fieldMetadata.getFieldName())) {
            return value!=null?positionNameMap.get(value):"未知";
        } else if ("marry".equals(fieldMetadata.getFieldName())) {
            if ("1".equals(value)) {
                return "是";
            } else if ("0".equals(value)) {
                return "否";
            } else {
                return "未知";
            }
        } else if ("departmentId".equals(fieldMetadata.getFieldName())) {

            return value!=null?departmentNameMap.get(value):"未知";
        }
        return value;
    }

    @Override
    public String buildWhereClause(LoginUser user, Map<String, String> queryParams, List<Object> args) {
        StringBuilder sql = new StringBuilder(" 1=1");
        if (StringUtils.isNotEmpty(queryParams.get("departmentId"))) {
            Map<String,Object> map = jdbcTemplate.queryForMap("select leftValue,rightValue from cw_Department where departmentId=?", queryParams.get("departmentId"));
            sql.append(" and departmentId in (select departmentId from cw_Department where leftValue between ? and ? )");
            args.add(map.get("leftValue"));
            args.add(map.get("rightValue"));
        }
        if (StringUtils.isNotEmpty(queryParams.get("positionId[]")) && !queryParams.get("positionId[]").contains("isAll")) {
            String[] position = queryParams.get("positionId[]").split(",");
            sql.append(" and positionId in (");

            for (int i = 0 ; i<position.length ;i++){
                if (i!=0){
                    sql.append(",");
                }
                sql.append("?");
                args.add(position[i]);
            }
            sql.append(")");
        }
        if (StringUtils.isNotEmpty(queryParams.get("marry")) && !"-1".equals(queryParams.get("marry"))) {
            sql.append(" and marry=?");
            args.add(queryParams.get("marry"));
        }
        if (StringUtils.isNotEmpty(queryParams.get("gender")) && !"-1".equals(queryParams.get("gender"))) {
            sql.append(" and gender=?");
            args.add(queryParams.get("gender"));
        }
        if (StringUtils.isNotEmpty(queryParams.get("employeeName"))) {
            sql.append(" and employeeName like ?");
            args.add("%" + queryParams.get("employeeName") + "%");
        }
        if (StringUtils.isNotEmpty(queryParams.get("fromDate"))) {
            sql.append(" and birthday >= ?");
            args.add(queryParams.get("fromDate"));
        }
        if (StringUtils.isNotEmpty(queryParams.get("toDate"))) {
            sql.append(" and birthday <= ?");
            args.add(queryParams.get("toDate"));
        }
        return sql.toString();
    }

    @Override
    public void prepareCall(LoginUser user, Map<String, String> queryParams, Map<String, Object> callParams) {

    }

    @Override
    public String getOperatorHtml(LoginUser user, Map<String, Object> rowData) {
        StringBuilder opt = new StringBuilder();
        opt.append("<a href='javascript:void(0);' onclick=\"editEmployee('" + rowData.get("employeeId") + "')\">编辑</a>");
        opt.append(" <a href='javascript:void(0);' onclick=\"delEmployee('" + rowData.get("employeeId") + "')\">删除</a>");
        return opt.toString();
    }

    @Override
    public boolean beforeQuery(QueryJdbcTemplate qjt, QueryMetadata qmd, Map<String, String> queryParams, QueryResult queryResult) {
        return true;
    }

    @Override
    public void doAfterQuery(QueryJdbcTemplate qjt, QueryMetadata qmd, Map<String, String> queryParams, QueryResult queryResult) {

    }



}
